package me.frank.tman.ui.stock;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.XPopup;

import java.util.List;

import me.frank.tman.R;
import me.frank.tman.model.StockExchangeModel;
import me.frank.tman.presenter.StockPresenter;
import me.frank.tman.util.Utils;

public class ExchangeRecordsFragment extends Fragment {
    private static final String TAG = ExchangeRecordsFragment.class.getSimpleName();

    private RecyclerView mExchangeRecordsRv;
    private ExchangeRecordsAdapter mExchangeRecordsAdapter;
    private StockPresenter mPresenter = StockPresenter.get();

    private TextView mHintText;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_stock, container, false);
        setHasOptionsMenu(true);
        mHintText = root.findViewById(R.id.tv_stock_hint);
        mExchangeRecordsRv = root.findViewById(R.id.rv_stock_list);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mExchangeRecordsRv.setLayoutManager(layoutManager);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        mExchangeRecordsRv.addItemDecoration(new SpacesItemDecoration(25));
        mExchangeRecordsRv.setItemAnimator(new DefaultItemAnimator());

        mExchangeRecordsAdapter = new ExchangeRecordsAdapter();
        mExchangeRecordsAdapter.setOnItemClickListener(new ExchangeRecordsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, StockExchangeModel record) {
                Log.d(TAG, "OnItemClick: " + record.toString());
                Bundle bundle = new Bundle();
                bundle.putString("stock_code", record.code);
                bundle.putString("exchange_type", record.exchangeType);
                NavHostFragment.findNavController(ExchangeRecordsFragment.this).navigate(R.id.action_to_trade, bundle);
            }

            @Override
            public void onItemLongClick(View view, StockExchangeModel record) {
                Log.d(TAG, "onItemLongClick: " + record.toString());
                new XPopup.Builder(getContext()).asConfirm("确认删除交易记录",
                        String.format("股票名称：%s\n股票代码: %s\n" +
                                        "交易类型：%s\n交易数量: %d\n" +
                                        "交易时间: %s", record.name, record.code, record.exchangeType, record.exchangeNum,
                                Utils.stampToDate(record.exchangeTime)),
                        () -> {
                            mPresenter.deleteExhangeRecord(record);
                            mExchangeRecordsAdapter.deleteItem(record);
                        })
                        .show();
            }
        });

        mExchangeRecordsRv.setAdapter(mExchangeRecordsAdapter);

        Bundle bundle = getArguments();
        if (bundle != null) {
            String stockCode = bundle.getString("stock_code");
            List<StockExchangeModel> exchangeModels = mPresenter.getStockExchanges(stockCode);
            if (exchangeModels == null || exchangeModels.size() <= 0) {
                mHintText.setVisibility(View.VISIBLE);
            } else {
                mHintText.setVisibility(View.GONE);
                mExchangeRecordsAdapter.setList(exchangeModels);
            }
        } else {
            mHintText.setVisibility(View.VISIBLE);
        }
        return root;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public boolean onBackPressed() {
        return NavHostFragment.findNavController(this).navigateUp();
    }
}